package com.freak.neteasecloudmusic.modules.find.recommend.base;

import com.freak.httphelper.ApiCallback;
import com.freak.httphelper.HttpMethods;
import com.freak.httphelper.RxPresenter;
import com.freak.httphelper.SubscriberCallBack;
import com.freak.neteasecloudmusic.app.api.ApiService;
import com.freak.neteasecloudmusic.commom.constants.Constants;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.NewAlbumEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.RecommendEveryDayEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.RecommendnewSongEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.BannerEntity;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.freak.neteasecloudmusic.utils.ToastUtil;
import com.google.gson.Gson;

import io.reactivex.Observable;

/**
 * Created by Administrator on 2019/3/15.
 */

public class RecommendPresenter extends RxPresenter<RecommendContract.View> implements RecommendContract.Presenter {
    ApiService mApiService = HttpMethods.getInstance().create(ApiService.class);

    @Override
    public void loadBanner() {
        Observable<BannerEntity> observable = mApiService.loadBanner();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<BannerEntity>() {
            @Override
            public void onSuccess(BannerEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    mView.loadBannerSuccess(model);
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        }));
    }

    @Override
    public void getRecommendEveryDay() {
        Observable<RecommendEveryDayEntity> observable = mApiService.getRecommendEveryDay();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<RecommendEveryDayEntity>() {
            @Override
            public void onSuccess(RecommendEveryDayEntity model) {

                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getRecommendEveryDaySuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getRecommendSongEveryDay() {
        Observable<Object> observable = mApiService.getRecommendSongEveryDay();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
            @Override
            public void onSuccess(Object model) {
                LogUtil.e(new Gson().toJson(model));
//                if (model.getCode() == Constants.SUCCESS_CODE) {
//                    if (mView==null){
//                        LogUtil.e("mView 为空");
//                    }else {
//                        mView.getUserInfoSuccess(model);
//                    }
//                } else {
//                    ToastUtil.shortShow("获取数据失败");
//                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getRecommendNewSong() {
        Observable<RecommendnewSongEntity> observable = mApiService.getRecommendNewSong();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<RecommendnewSongEntity>() {
            @Override
            public void onSuccess(RecommendnewSongEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getRecommendNewSongSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getNewEst() {
        Observable<NewAlbumEntity> observable = mApiService.getNewEst();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<NewAlbumEntity>() {
            @Override
            public void onSuccess(NewAlbumEntity model) {
                LogUtil.e(new Gson().toJson(model));
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getNewEstSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getTopNewEst(int offset, int limit) {
        Observable<NewAlbumEntity> observable = mApiService.getTopNewEst(offset,limit);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<NewAlbumEntity>() {
            @Override
            public void onSuccess(NewAlbumEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getTopNewEstSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }
}
